<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="warning" size="small" icon="el-icon-download" @click="$refs.exportComp.visible = true" v-if="permission.rawMaterialLotNoInventory_export"
          >导出</el-button
        >
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button type="text" size="small" @click="viewRunningWaterDetail(scope.row)" v-if="permission.receiving_export">包装详情</el-button>
      </template>
    </avue-crud>
    <el-dialog v-dragDirectives ref="dialog" title="包装详情" append-to-body :visible.sync="showDialog" :close-on-click-modal="false" top="5vh" width="70%" @close="closeDialog">
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>物料信息</h3>
      <avue-form v-model="detailFormInfo" :option="detailFormOption"></avue-form>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>包装详情</h3>
      <avue-crud
        :option="detailTableOption"
        :table-loading="detailTableLoading"
        :data="detailTableData"
        :page.sync="detailTablePage"
        @current-change="detailTableCurrentChange"
        @size-change="detailTableSizeChange"
      >
      </avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-circle-close" @click="showDialog = false">取 消</el-button>
      </span>
    </el-dialog>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/inventory-lot/page" :option="option" :page="page" :query="query" file-name="原材料批次库存"></export-comp>
  </basic-container>
</template>

<script>
import { getList, getInventoryLog } from '@/api/warehouse/rawMaterialLotNoInventory';
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp/index';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  components: {
    exportComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      query: {},
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 360px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        border: true,
        index: true,
        indexLabel: '序号',
        selection: true,
        dialogClickModal: false,
        searchLabelWidth: 100,
        searchSpan: 6,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        menu: true,
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料型号',
            prop: 'partModel',
            search: true
          },
          {
            label: '物料品牌',
            prop: 'partBrand'
          },
          {
            label: '内部批次',
            prop: 'lotNo',
            search: true
          },
          {
            label: '批次数量',
            prop: 'quantity'
          },
          {
            label: '锁定数量',
            prop: 'lockedQuantity'
          },
          {
            label: '可用数量',
            prop: 'availableQuantity'
          }
        ]
      },
      data: [],
      detailFormOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            span: 8,
            disabled: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            span: 8,
            disabled: true
          },
          {
            label: '物料型号',
            prop: 'partModel',
            span: 8,
            disabled: true
          },
          {
            label: '物料品牌',
            prop: 'partBrand',
            span: 8,
            disabled: true
          },
          {
            label: '内部批次',
            prop: 'lotNo',
            span: 8,
            disabled: true
          },
          {
            label: '批次数量',
            prop: 'quantity',
            span: 8,
            disabled: true
          }
        ]
      },
      showDialog: false,
      detailFormInfo: {},
      detailTableOption: {
        height: 300,
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 4,
        searchMenuPosition: 'right',
        border: true,
        index: true,
        indexLabel: '序号',
        selection: false,
        dialogClickModal: false,
        searchLabelWidth: 100,
        searchSpan: 6,
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        refreshBtn: false,
        menu: false,
        menuWidth: 100,
        columnBtn: false,
        column: [
          {
            label: '包装条码',
            prop: 'packingSn'
          },
          {
            label: '包装数量',
            prop: 'packingQuantity'
          },
          {
            label: '供应商',
            prop: 'vendorCode'
          },
          {
            label: '供应商批次',
            prop: 'vendorLotNo'
          },
          {
            label: '仓库',
            prop: 'warehouseName'
          },
          {
            label: '区域',
            prop: 'areaName'
          },
          {
            label: '储位',
            prop: 'storageName'
          }
        ]
      },
      detailTableData: [],
      detailTablePage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      detailTableLoading: false,
      rowId: ''
    };
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.receiving_view, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  created() {},
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.receiveTime && params.receiveTime.length > 0) {
        params.queryStartTime = params.receiveTime[0];
        params.queryEndTime = params.receiveTime[1];
        delete params.receiveTime;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    viewRunningWaterDetail(row) {
      this.showDialog = true;
      this.detailFormInfo = row;
      this.rowId = row.id;
      this.queryInventoryLog();
    },
    queryInventoryLog() {
      this.detailTableLoading = true;
      getInventoryLog(this.detailTablePage.currentPage, this.detailTablePage.pageSize, { inventoryLotId: this.rowId }).then((res) => {
        const data = res.data.data;
        this.detailTablePage.total = data.total;
        this.detailTableData = data.records;
        this.detailTableLoading = false;
      });
    },
    detailTableCurrentChange(currentPage) {
      this.detailTablePage.currentPage = currentPage;
      this.queryInventoryLog();
    },
    detailTableSizeChange(pageSize) {
      this.detailTablePage.pageSize = pageSize;
      this.queryInventoryLog();
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  }
};
</script>

<style lang="scss" scoped>
.dialog-content {
  height: 70vh;
  overflow-y: scroll;
}
::v-deep .el-dialog__header {
  padding: 16px 24px;
  min-height: 20px;
  border-bottom: 1px solid #f0f0f0;
}
.el-dialog__wrapper {
  ::v-deep .avue-crud__menu {
    display: none;
  }
}
</style>
